

<?php

class AdsController extends Zend_Controller_Action {
    
    public function parseAction(){
        $this->_helper->viewRenderer->setNoRender(true);   
        $url = $this->_request->getParam("url") == "" ? "" : $this->_request->getParam("url");
        
        $client = new Zend_Http_Client();
        $client->setUri($url);
        $response = $client->request();
        $html = $response->getBody();

        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.imgDetailArticle');
        $data = array();

        $results->rewind();
        
        
        while($results->valid()){
            $domDoc = $results->current();
            echo $domDoc->getElementsByTagName('img')->item(0)->getAttribute('src').'</br>';
            
            $results->next();
        }
    }
    
    
    const KEY_CACHED = "ads_cached";
    private static $memcache;
    
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
        parent::__construct($request, $response, $invokeArgs);
        
        if(Haibl_ConfigData::SUPPORT_MEMCACHED){
            self::$memcache = new Memcache;
            self::$memcache->connect(Haibl_ConfigData::DOMAIN, Haibl_ConfigData::PORT) or die ("Could not connect");
        }        
    }

    public function indexAction() {
        $this->_helper->viewRenderer->setNoRender(true);   
        $data['errorId'] = 0;
        $data['data'] = array();
        echo json_encode($data);
        
        
//        $index = $this->_request->getParam("index");
//        if ($index == "")
//            $index = 0;
//        
//        $memCachedData = null;
//        if(Haibl_ConfigData::SUPPORT_MEMCACHED) $memCachedData = self::$memcache->get(self::KEY_CACHED);
//        $ads = null;
//        if ($memCachedData != null) {
//            $ads = $this->processAds($memCachedData,$index);
//        } else {
//            $sql = "Select * 
//                    from ads 
//                    order by priority DESC";
//            $db = Model_Connections::getInstance()->getDbConnect();
//            $data = $db->fetchAll($sql);
//            if (isset($data) && count($data) > 0) {
//                if (Haibl_ConfigData::SUPPORT_MEMCACHED) {
//                    //dang sap xep theo do uu tien cang cao thi nam o dau 
//                    self::$memcache->set(self::KEY_CACHED, $data, false, Haibl_ConfigData::CACHED_KEEP_ALIVE) or die("Failed to save data at the server");
//                }                
//                
//                for($i = 0 ; $i < count($data) ; $i++){
//                    $data[$i]['content'] = base64_encode($data[$i]['content']);
//                }
//                
//                $ads['data'] = $data;
//                echo json_encode($ads);                 
//            } else {
//                $googleAds['name'] = 'googleMob';
//                $googleAds['type'] = '-1';
//                echo json_encode($googleAds);
//            }
//        }
    }
    
    private function processAds($memCachedData, $index) {
        if (isset($memCachedData) && count($memCachedData) > 0) {            
            return $memCachedData[$index];
        }
    }
}
?>

